package server;

import java.io.IOException;
import java.io.InputStream;

public class ReceiveThread extends Thread {
	private Participant p;
	private InputStream in;
	private ReceiveMonitor rm;
	private boolean run = true;

	public ReceiveThread(Participant p, ReceiveMonitor rm) {
		this.p = p;
		this.rm = rm;
		try {
			in = p.getSocket().getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {

		while (run) {
			byte[] buffer = new byte[65535];
			try {
				in.read(buffer);
				String message = new String(buffer, 0, buffer.length);
				rm.setMessage(message.trim(), p.getName());
			} catch (IOException e) {
				run = false;
			}
		}
	}

	public void setReceiveMonitor(ReceiveMonitor rm) {
		this.rm = rm;
	}

	public Participant getParticipant() {
		return p;
	}

	public boolean equals(ReceiveThread rt) {
		return p.equals(rt.getParticipant());
	}

}
